package com.memory.servlet;

import com.memory.dao.impl.UserDaoImpl;
import com.memory.pojo.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * ClassName: login
 * Package: com.memory.servlet
 * Description:
 *
 * @Author Memory
 * @Create 2024/8/14 14:31
 * @Version 1.0
 */
@WebServlet(value = "/SelectAllServlet" )
public class SelectAllServlet extends HttpServlet {
    UserDaoImpl userDao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.完成查询
        List<User> userList = null;
        try {
            userList = userDao.selectAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        // 2.存入request域中
        request.setAttribute("userList", userList);

        // 3.转发到userList.jsp
        // request.getRequestDispatcher("/index.jsp").forward(request, response);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/userList.jsp");

        requestDispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
